﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Facade;
using IronOne.SalesOne.BusinessObjects;

namespace IronOne.SalesOne.SalesOneWeb.ContactManagement
{
    public partial class ProspectDetails : SOSecurityPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                CustomerFacade cusFac = new CustomerFacade();

                if (!IsPostBack)
                {
                    ValidateUserCredentials(Request.Url.LocalPath);
                    ViewState["refUrl"] = Request.UrlReferrer.ToString();

                    if (!Page.IsPostBack && Request.UrlReferrer.ToString().Contains("ActivityPlan.aspx"))
                        Session["planQueryString"] = Request.UrlReferrer.Query.ToString();
                }

                if (!string.IsNullOrEmpty(Request.QueryString["t"]))
                {
                    if (Request.QueryString["t"] == "v")
                    {
                        btnNeedAn.Text = "View Needs Analysis";
                    }
                    else
                    {
                        btnNeedAn.Text = "Update Needs Analysis";
                    }

                    btnNeedAn.Visible = true;
                }
                else
                {
                    btnNeedAn.Visible = false;
                }


                if (!string.IsNullOrEmpty(Request.QueryString["id"]))
                {
                    string prospectId = Request.QueryString["id"].ToString();

                    Customer cus = cusFac.GetCustomer(prospectId);


                    if (!string.IsNullOrEmpty(Request.QueryString["nid"]))
                    {
                        if (!IsPostBack)
                        {
                            if (Session["naRow"] != null)
                            {
                                int nid = Convert.ToInt32(Request.QueryString["nid"]);
                                GridViewRow rw = (GridViewRow)Session["naRow"];

                                SalesOne.BusinessObjects.NeedsAnalysis na = cusFac.GetNeedAnalysis(nid);
                                na.NeedAnalysisNo = nid;
                                na.ProspectId = prospectId;

                                //income
                                na.Inc = rw.Cells[26].Text == null ? 0 : (float)Convert.ToDouble(rw.Cells[26].Text);
                                na.SpouseIncome = rw.Cells[27].Text == null ? 0 : (float)Convert.ToDouble(rw.Cells[27].Text);
                                na.OtherIncome = rw.Cells[28].Text == null ? 0 : (float)Convert.ToDouble(rw.Cells[28].Text);
                                na.SavingInc = rw.Cells[29].Text == null ? 0 : (float)Convert.ToDouble(rw.Cells[29].Text);
                                na.Income = rw.Cells[24].Text == null ? 0 : (float)Convert.ToDouble(rw.Cells[24].Text);

                                //liability
                                na.UnpLoans = rw.Cells[29].Text == null ? 0 : (float)Convert.ToDouble(rw.Cells[29].Text);
                                na.Liability = rw.Cells[23].Text == null ? 0 : (float)Convert.ToDouble(rw.Cells[23].Text);

                                //expenses
                                na.Food = rw.Cells[31].Text == null ? 0 : (float)Convert.ToDouble(rw.Cells[31].Text);
                                na.HouseExp = rw.Cells[32].Text == null ? 0 : (float)Convert.ToDouble(rw.Cells[32].Text);
                                na.Trans = rw.Cells[33].Text == null ? 0 : (float)Convert.ToDouble(rw.Cells[33].Text);
                                na.SchFees = rw.Cells[34].Text == null ? 0 : (float)Convert.ToDouble(rw.Cells[34].Text);
                                na.OtherExp = rw.Cells[35].Text == null ? 0 : (float)Convert.ToDouble(rw.Cells[35].Text);
                                na.LoanIns = rw.Cells[36].Text == null ? 0 : (float)Convert.ToDouble(rw.Cells[36].Text);
                                na.CredPay = rw.Cells[37].Text == null ? 0 : (float)Convert.ToDouble(rw.Cells[37].Text);
                                na.Expenses = rw.Cells[17].Text == null ? 0 : (float)Convert.ToDouble(rw.Cells[17].Text);

                                //assets
                                na.FixedDep = rw.Cells[38].Text == null ? 0 : (float)Convert.ToDouble(rw.Cells[38].Text);
                                na.Saving = rw.Cells[39].Text == null ? 0 : (float)Convert.ToDouble(rw.Cells[39].Text);
                                na.OthCey = rw.Cells[40].Text == null ? 0 : (float)Convert.ToDouble(rw.Cells[40].Text);
                                na.OthIns = rw.Cells[41].Text == null ? 0 : (float)Convert.ToDouble(rw.Cells[41].Text);
                                na.OthAss = rw.Cells[42].Text == null ? 0 : (float)Convert.ToDouble(rw.Cells[42].Text);
                                na.Assets = rw.Cells[21].Text == null ? 0 : (float)Convert.ToDouble(rw.Cells[21].Text);

                                //spouse
                                na.SpId = rw.Cells[43].Text == "&nbsp;" ? string.Empty : rw.Cells[43].Text;
                                na.SpName = rw.Cells[44].Text == "&nbsp;" ? string.Empty : rw.Cells[44].Text;
                                na.SpOtherName = rw.Cells[45].Text == "&nbsp;" ? string.Empty : rw.Cells[45].Text;
                                na.SpDob = rw.Cells[46].Text == "&nbsp;" ? string.Empty : rw.Cells[46].Text;
                                na.SpGender = rw.Cells[47].Text == "&nbsp;" ? string.Empty : rw.Cells[47].Text;
                                na.SpProfession = rw.Cells[48].Text == "&nbsp;" ? string.Empty : rw.Cells[48].Text;

                                na.IdNo = rw.Cells[3].Text == "&nbsp;" ? string.Empty : rw.Cells[3].Text;
                                na.OtherName = rw.Cells[54].Text == "&nbsp;" ? string.Empty : rw.Cells[54].Text;
                                na.Profession = rw.Cells[55].Text == "&nbsp;" ? string.Empty : rw.Cells[55].Text;
                                na.Address1 = rw.Cells[56].Text == "&nbsp;" ? string.Empty : rw.Cells[56].Text;
                                na.Address2 = rw.Cells[57].Text == "&nbsp;" ? string.Empty : rw.Cells[57].Text;
                                na.City = rw.Cells[58].Text == "&nbsp;" ? string.Empty : rw.Cells[58].Text;
                                na.Country = rw.Cells[59].Text == "&nbsp;" ? string.Empty : rw.Cells[59].Text;
                                na.HomePhone = rw.Cells[60].Text == "&nbsp;" ? string.Empty : rw.Cells[60].Text;
                                na.OfficePhone = rw.Cells[61].Text == "&nbsp;" ? string.Empty : rw.Cells[61].Text;
                                na.Mobile = rw.Cells[62].Text == "&nbsp;" ? string.Empty : rw.Cells[62].Text;
                                na.Email = rw.Cells[63].Text == "&nbsp;" ? string.Empty : rw.Cells[63].Text;
                                na.PrefPlace = rw.Cells[65].Text == "&nbsp;" ? string.Empty : rw.Cells[65].Text;
                                na.PrefCall = rw.Cells[64].Text == "&nbsp;" ? string.Empty : rw.Cells[64].Text;
                                na.ProsRem = rw.Cells[66].Text == "&nbsp;" ? string.Empty : rw.Cells[66].Text;
                                



                                lblProspectId.Text = prospectId;
                                lblName.Text = na.LastName;
                                lblDob.Text = na.Dob;
                                lblMaritalStatus.Text = na.Marital;
                                lblAddress1.Text = na.Address1;
                                lblAddress2.Text = na.Address2;
                                lblCity.Text = na.City;
                                lblCountry.Text = na.Country;
                                lblPreferredNotoCall.Text = na.PrefCall;
                                lblRemarks.Text = na.ProsRem;
                                lblId.Text = na.IdNo;
                                lblOtherName.Text = na.OtherName;
                                lblGender.Text = na.Gender;
                                lblProfession.Text = na.Profession;
                                lblHomePhone.Text = na.HomePhone;
                                lblOfficePhone.Text = na.OfficePhone;
                                lblMobilePhone.Text = na.Mobile;
                                lblEmail.Text = na.Email;
                                lblPreferredPlaceToMeet.Text = na.PrefPlace;

                                Session.Remove("naRow");
                                Session["na"] = na;
                            }
                            else if (Session["na"] != null)
                            {
                                SalesOne.BusinessObjects.NeedsAnalysis na = (SalesOne.BusinessObjects.NeedsAnalysis)Session["na"];

                                lblProspectId.Text = prospectId;
                                lblName.Text = na.LastName;
                                lblDob.Text = na.Dob;
                                lblMaritalStatus.Text = na.Marital;
                                lblAddress1.Text = na.Address1;
                                lblAddress2.Text = na.Address2;
                                lblCity.Text = na.City;
                                lblCountry.Text = na.Country;
                                lblPreferredNotoCall.Text = na.PrefCall;
                                lblRemarks.Text = na.ProsRem;
                                lblId.Text = na.IdNo;
                                lblOtherName.Text = na.OtherName;
                                lblGender.Text = na.Gender;
                                lblProfession.Text = na.Profession;
                                lblHomePhone.Text = na.HomePhone;
                                lblOfficePhone.Text = na.OfficePhone;
                                lblMobilePhone.Text = na.Mobile;
                                lblEmail.Text = na.Email;
                                lblPreferredPlaceToMeet.Text = na.PrefPlace;
                            }

                        }

                        //rw.Cells[
                    }
                    else
                    {

                        //Prospect pros = cusFac.GetProspect(prospectId);

                        lblProspectId.Text = cus.CustomerId;
                        lblName.Text = cus.FirstName;
                        lblDob.Text = cus.DOB;
                        lblMaritalStatus.Text = cus.MaritalStatus;
                        lblAddress1.Text = cus.Address1;
                        lblAddress2.Text = cus.Address2;
                        lblCity.Text = cus.City;
                        lblCountry.Text = cus.Country;
                        lblPreferredNotoCall.Text = cus.PrefNo;
                            //"";
                        lblRemarks.Text = cus.Remarks;
                        lblId.Text = cus.IdNumber;
                        lblOtherName.Text = cus.OtherName;
                        lblGender.Text = cus.Gender;
                        lblProfession.Text = cus.Profession;
                        lblHomePhone.Text = cus.HomeTelephone;
                        lblOfficePhone.Text = cus.OfficialTelephone;
                        lblMobilePhone.Text = cus.Mobile;
                        lblEmail.Text = cus.EMail;
                        lblPreferredPlaceToMeet.Text = cus.PrefPlace;
                            //"";


                        Session["cusId"] = cus.CustomerId;
                        Session["prospectId"] = prospectId;
                    }



                }
            }
            catch (Exception)
            {

            }
            
            

        }

        protected void TextBox18_TextChanged(object sender, EventArgs e)
        {

        }

        protected void viewFinanceButton_Click(object sender, EventArgs e)
        {
            try
            {
                if (!string.IsNullOrEmpty(Request.QueryString["nid"]))
                {
                    string nid = Request.QueryString["nid"];
                    Response.Redirect("~/ContactManagement/FinanceDetails.aspx?nid=" + nid);
                }
                else
                {
                    Response.Redirect("~/ContactManagement/FinanceDetails.aspx");
                }
            }
            catch (Exception)
            {
                
            }
            
            
        }

        protected void viewSpouseButton_Click(object sender, EventArgs e)
        {
            CustomerFacade cFac = new CustomerFacade();
            try
            {
                
                    if (!string.IsNullOrEmpty(Request.QueryString["nid"]))
                    {
                        string nid = Request.QueryString["nid"];
                        
                        Response.Redirect("~/ContactManagement/SpouseDetails.aspx?nid=" + nid, false);
                                              
                    }
                    else
                    {
                        if (cFac.GetSpouseForCustomer(Session["cusId"].ToString()) != null)
                        {
                            Response.Redirect("~/ContactManagement/SpouseDetails.aspx", false);
                        }
                        else
                        {
                            MassageSummery("IIS504");
                        }
                        
                    }

                
                
                

            }
            catch (Exception)
            {

                MassageSummery("IIS504");
            }
            
            
            
        }

        protected void viewDependantButton_Click(object sender, EventArgs e)
        {
            CustomerFacade cFac = new CustomerFacade();

            try
            {
               
                   if (!string.IsNullOrEmpty(Request.QueryString["nid"]))
                    {
                        string nid = Request.QueryString["nid"];
                        if (cFac.GetDependentsForNa(int.Parse(nid), 1).Count > 0)
                        {
                            Response.Redirect("~/ContactManagement/ViewDependent.aspx?nid=" + nid);
                        }
                        else
                        {
                            MassageSummery("IIS504");
                        }
                        
                    }
                    else
                    {
                        if (cFac.GetDependentsForCustomer(Session["cusId"].ToString(),1).Count >0)
                        {
                            Response.Redirect("~/ContactManagement/ViewDependent.aspx");
                        }
                        else
                        {
                            MassageSummery("IIS504");
                        }
                        
                    } 
                
                
            }
            catch (Exception)
            {

                MassageSummery("IIS504");
            }
            
            
        }

        protected void cancelButton_Click(object sender, EventArgs e)
        {
            try
            {
                if (!string.IsNullOrEmpty(Request.QueryString["t"]))
                {
                    Response.Redirect("~/NeedsAnalysis/SearchNeedsAnalysis.aspx");
                }
                else if (Session["planQueryString"] != null)
                {
                    Response.Redirect("~/SalesActivity/ActivityPlan.aspx" + Session["planQueryString"].ToString(),false);
                    Session.Remove("planQueryString");
                }
                else
                {
                    Response.Redirect("~/ContactManagement/SearchContact.aspx");
                }

            }
            catch (Exception)
            {
                
            }
           
            
            //Response.Redirect(ViewState["refUrl"].ToString());
        }

        protected void btnNeedAn_Click(object sender, EventArgs e)
        {
            try
            {
                string nid = string.Empty;

                if (!string.IsNullOrEmpty(Request.QueryString["nid"]))
                {
                    nid = Request.QueryString["nid"];
                }

                if (Request.QueryString["t"] == "v")
                {
                    Response.Redirect("~/NeedsAnalysis/NeedsAnalysisDetails.aspx?nid=" + nid + "&a=v",false);
                }
                else
                {
                    Response.Redirect("~/NeedsAnalysis/NeedsAnalysisDetails.aspx?nid=" + nid + "&a=u", false);
                }
            }
            catch (Exception)
            {
                
                throw;
            }
            
        }
    }
}
